package com.awspaas.user.apps.sts.controller;

import com.actionsoft.bpms.bo.engine.BO;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.ProcessInstance;
import com.actionsoft.bpms.bpmn.engine.model.run.delegate.TaskInstance;
import com.actionsoft.bpms.commons.database.RowMap;
import com.actionsoft.bpms.org.model.UserModel;
import com.actionsoft.bpms.server.bind.annotation.Controller;
import com.actionsoft.bpms.server.bind.annotation.HandlerType;
import com.actionsoft.bpms.server.bind.annotation.Mapping;
import com.actionsoft.bpms.server.bind.annotation.Param;
import com.actionsoft.bpms.util.DBSql;
import com.actionsoft.sdk.local.SDK;
import com.alipay.remoting.util.StringUtils;
import com.alibaba.fastjson.JSON;
import com.awspaas.user.apps.publicservice.bean.*;
import com.awspaas.user.apps.sts.service.STSService;

import java.util.Map;


/**
 * @Title: ScheduleRecall
 * @Package com.awspaas.user.apps.sts.controller
 * @Description:
 * @Author wanglx
 * @Date 2022/7/26
 */

@Controller(type = HandlerType.OPENAPI, apiName = "TASKSYNC API", desc = "任务集成接口（通用）")
public class STSServerController {

    //流程启动接口
    @Mapping(value = "TASKSYNC.startProcess",desc = "流程启动接口")
    public Response startProcess(@Param(value = "applicationName", desc = "系统标识", required = true) String applicationName,
                                 @Param(value = "targetUid", desc = "流程创建人ID", required = true) String targetUid,
                                 @Param(value = "outerProcessDefId", desc = "流程定义ID", required = true) String outerProcessDefId,
                                 @Param(value = "outerProcessName", desc = "流程名称", required = true) String outerProcessName,
                                 @Param(value = "outerTitle", desc = "流程标题", required = false) String outerTitle,
                                 @Param(value = "businessID", desc = "业务ID", required = false) String businessID,
                                 @Param(value = "TrustContractID", desc = "信托合同ID", required = false) String TrustContractID,
                                 @Param(value = "clientID", desc = "委托人ID", required = false) String clientID,
                                 @Param(value = "projectID", desc = "项目ID", required = false) String projectID,
                                 @Param(value = "transactionOrderID", desc = "交易单ID", required = false) String transactionOrderID,
                                 @Param(value = "investmentContractID", desc = "投资合同ID", required = false) String investmentContractID,
                                 @Param(value = "extInfo", desc = "扩展信息", required = false) Map extInfo)
    {

        System.out.println("流程启动接口开始调用......");
                String defId = SDK.getAppAPI().getProperty("com.awspaas.user.apps.sts", "CL_DEF_ID");
        Boolean isHasDefId = Boolean.valueOf(false);
        if (StringUtils.isNotBlank(defId)) {
            int index = defId.indexOf(outerProcessDefId);
            if (index >= 0)
                isHasDefId = Boolean.valueOf(true);
        }
        Response response = new Response();
        RequestData requestData = new RequestData();
        requestData.setApplicationName(applicationName);
        requestData.setTargetUid(targetUid);
        requestData.setOuterProcessDefId(outerProcessDefId);
        requestData.setOuterProcessName(outerProcessName);
        requestData.setProcessTitle(outerTitle);
        requestData.setBusinessId(businessID);
        requestData.setTrustContractID(TrustContractID);
        requestData.setClientID(clientID);
        requestData.setProjectID(projectID);
        requestData.setTransactionOrderID(transactionOrderID);
        requestData.setInvestmentContractID(investmentContractID);
        requestData.setExtInfo(extInfo);
        ResponseData responseData = new ResponseData();
        AwsLog awsLog = AwsLog.awsLog(applicationName,"TASKSYNC.startProcess","流程启动接口",targetUid,targetUid,JSON.toJSONString(requestData),applicationName,"","",businessID, TrustContractID,clientID,projectID,transactionOrderID,investmentContractID ,JSON.toJSONString(extInfo));
        if("FTS".equals(applicationName)){
            startFtsProcess(requestData, response, awsLog, responseData);
        } else {
            SDK.getLogAPI().consoleInfo("是否进入并联发起流程====扩展信息="+ extInfo);
            startOtherProcess(requestData, response, awsLog, responseData);
        }
        return response;
    }


    private   Response startOtherProcess(RequestData requestData,Response response,AwsLog awsLog , ResponseData responseData){
        STSService service = new STSService();
        Map<String, String> extInfo = requestData.getExtInfo();
        String targetUid = requestData.getTargetUid();

        int num = 0;

        String code = null;
        String desc = null;

        //查询创建人是否存在本系统中
        String  USERID = DBSql.getString("SELECT USERID FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});
        if(USERID != "" && !USERID.equals("")){
            targetUid = USERID;
            requestData.setTargetUid(targetUid);
        }else{
            //返回Reponse信息
            code = "500";
            desc = "BPM系统不存在创建人账户";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);

        }

        Map<String, String> responseExtInfo = null;

        responseData.setExtInfo(responseExtInfo);

        try {
            /*RowMap rowMap = DBSql.getMap("SELECT * FROM BO_PS_STS_PROCESS_MAPPING WHERE STS_PRODEFID = ?", new Object[]{outerProcessDefId});
            if (rowMap == null){
                //返回Reponse信息
                code = "500";
                desc = "BPM系统不存在此流程";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            }else{*/
            //查询创建人是否存在本系统中
            num = DBSql.getInt("SELECT COUNT(*) FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});
            if (num == 0){
                //返回Reponse信息
                code = "500";
                desc = "BPM系统不存在创建人账户";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            }else{
                   /* //BPM流程定义ID
                    String BPM_PRODEFID = rowMap.getString("BPM_PRODEFID");*/
                //返回Reponse信息
                responseData = service.startProcess(requestData);
                String responseDataOuterId = responseData.getOuterId();
                if (null!=responseDataOuterId && !responseDataOuterId.equals("")) {
                    ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                    response.setCode(successRC.getCode());
                    response.setDesc(successRC.getDesc());
                    response.setData(responseData);
                    SDK.getLogAPI().consoleInfo(JSON.toJSONString(extInfo));
                    awsLog.end(AwsLog.SUCCESS, response);
                } else {
                    code = "200";
                    desc = "当前流程已结束";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.SUCCESS, response);
                }
            }
        } catch (Exception e){
            e.printStackTrace();
            ResponseCode failRC = ResponseCode.FAIL_EXCEPTION;
            response.setCode(failRC.getCode());
            response.setDesc(failRC.getDesc());
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);
        } finally {

            // 将错误信息存储至接口日志表中
            BO awsBo = awsLog.getBo();
            if(awsBo != null) {
                awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                awsBo.set("OUTPARAMETER",response.toString());

                // 创建一条流程实例，用于存储DW查看
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.startProcess接口");
                if (num == 0) {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), "admin");
                } else {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), targetUid);
                }
            }else{
                awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                awsBo.set("OUTPARAMETER",response.toString());

                // 创建一条流程实例，用于存储DW查看
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.startProcess接口");
                if (num == 0) {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), "admin");
                } else {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), targetUid);
                }
            }



            return response;
        }
    }


    private   Response startFtsProcess(RequestData requestData,Response response,AwsLog awsLog , ResponseData responseData){
        STSService service = new STSService();
        Map<String, String> extInfo = requestData.getExtInfo();
        String targetUid = requestData.getTargetUid();

        int num = 0;

        String code = null;
        String desc = null;

        //查询创建人是否存在本系统中
        String  USERID = DBSql.getString("SELECT USERID FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});
        if(USERID != "" && !USERID.equals("")){
            targetUid = USERID;
            requestData.setTargetUid(targetUid);
        }else{
            //返回Reponse信息
            code = "500";
            desc = "BPM系统不存在创建人账户";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);

        }

        Map<String, String> responseExtInfo = null;

        responseData.setExtInfo(responseExtInfo);



            //查询创建人是否存在本系统中
            num = DBSql.getInt("SELECT COUNT(*) FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});
            if (num == 0){
                //返回Reponse信息
                code = "500";
                desc = "BPM系统不存在创建人账户";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            }else{
                   /* //BPM流程定义ID
                    String BPM_PRODEFID = rowMap.getString("BPM_PRODEFID");*/
                //返回Reponse信息
                responseData = service.startProcess(requestData,awsLog,response);
                String responseDataOuterId = responseData.getOuterId();
                if (null!=responseDataOuterId && !responseDataOuterId.equals("")) {
                    ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                    response.setCode(successRC.getCode());
                    response.setDesc(successRC.getDesc());
                    response.setData(responseData);
                    SDK.getLogAPI().consoleInfo(JSON.toJSONString(extInfo));
                    awsLog.end(AwsLog.SUCCESS, response);
                } else {
                    code = "200";
                    desc = "当前流程已结束";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.SUCCESS, response);
                }
            }

        return response;

    }


    //流程办理接口
    @Mapping(value = "TASKSYNC.completeProcess",desc = "流程办理接口")
    public Response completeProcess(@Param(value = "applicationName", desc = "系统标识", required = true) String applicationName,
                                  @Param(value = "processInstId", desc = "流程实例ID", required = true) String processInstId,
                                  @Param(value = "targetUid", desc = "流程办理人ID", required = true) String targetUid,
                                  @Param(value = "actionName", desc = "操作", required = true) String actionName,
                                  @Param(value = "outerTitle", desc = "任务标题", required = false) String outerTitle,
                                  @Param(value = "businessID", desc = "业务ID", required = false) String businessID,
                                  @Param(value = "extInfo", desc = "扩展信息", required = false) Map extInfo)
    {

        //定义返回值
        Response response = new Response();
        STSService service = new STSService();
        ResponseData responseData = new ResponseData();
        //将所有参数放入map中
        RequestData requestData = new RequestData();
        AwsLog awsLog = AwsLog.awsLog(applicationName,"TASKSYNC.completeProcess", "流程办理接口",
                targetUid, null, JSON.toJSONString(requestData), applicationName, processInstId, null,businessID,"","","","","", JSON.toJSONString(extInfo));



        requestData.setApplicationName(applicationName);
        requestData.setProcessInstId(processInstId);
        requestData.setTargetUid(targetUid);
        requestData.setActionName(actionName);
        requestData.setExtInfo(extInfo);
        requestData.setTaskTitle(outerTitle);
        requestData.setBusinessId(businessID);



        String code = null;
        String desc = null;
        Map<String, String> responseExtInfo = null;

        responseData.setExtInfo(responseExtInfo);


        int num = 0;
        //查询创建人是否存在本系统中
        String  USERID = DBSql.getString("SELECT USERID FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});
        if(USERID != "" && !USERID.equals("")){
            targetUid = USERID;
        }else{
            //返回Reponse信息
            code = "500";
            desc = "BPM系统不存在创建人账户";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);
        }



        //查询创建人是否存在本系统中
        num = DBSql.getInt("SELECT COUNT(*) FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});


        try {
            //获取当前节点信息
            /*TaskInstance taskInstance = SDK.getTaskAPI().getTaskInstance(outerId);
            SDK.getTaskAPI().get
            if (null!=taskInstance) {
                if (!taskInstance.isHistoryTask()) {
                    //执行人账户名
                    String target = taskInstance.getTarget();
                    if (!target.equals(targetUid)){
                        code = "500";
                        desc = targetUid+"不是当前办理人";
                        response.setCode(code);
                        response.setDesc(desc);
                        response.setData(responseData);
                        awsLog.end(AwsLog.ERROR, response);
                    }else{
                        if (SDK.getTaskAPI().isClaimTask(taskInstance)){
                            //先要认领
                            taskInstance = SDK.getTaskAPI().claimTask(outerId, targetUid);
                        }
                        ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                        response.setCode(successRC.getCode());
                        response.setDesc(successRC.getDesc());
                        responseData = service.completeProcess(requestData, taskInstance);
                        response.setData(responseData);
                    }
                } else {
                    code = "500";
                    desc = outerId+"为已结束任务";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.ERROR, response);
                }
            } else {
                code = "500";
                desc = outerId+"不是任务实例id";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            }*/
            RowMap taskRowMap = DBSql.getMap("SELECT * FROM WFC_TASK WHERE PROCESSINSTID = ? AND TASKSTATE = ? AND CONTROLSTATE = ?", new Object[]{processInstId, 1, "active"});
            SDK.getLogAPI().consoleInfo("待办rowmap"+taskRowMap);
            if (null==taskRowMap || taskRowMap.isEmpty()) {
                code = "500";
                desc = "该流程实例ID："+processInstId+"无待办任务";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            } else {
                String taskInstID = taskRowMap.getString("ID");
                TaskInstance taskInstance = SDK.getTaskAPI().getTaskInstance(taskInstID);
                if (SDK.getTaskAPI().isClaimTask(taskInstance)){
                    //先要认领
                    taskInstance = SDK.getTaskAPI().claimTask(taskInstID, targetUid);
                }
                ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                response.setCode(successRC.getCode());
                response.setDesc(successRC.getDesc());
                // System.out.println("任务完成人：" + taskInstance.getTarget());
                responseData = service.completeProcess(requestData, taskInstance,awsLog);
                // System.out.println("返回值：111" + response);
                String responseDataOuterId = responseData.getOuterId();
                response.setData(responseData);
                awsLog.end(AwsLog.SUCCESS,response);
            }
        }catch (Exception e){
            code = "500";
            desc = "流程办理错误";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);
        } finally {
            // 将错误信息存储至接口日志表中
            BO awsBo = awsLog.getBo();

            if(awsBo != null) {
                awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                awsBo.set("OUTPARAMETER",response.toString());

                // 创建一条流程实例，用于存储DW查看
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.completeProcess接口");
                if (num == 0) {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), "admin");
                } else {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), targetUid);
                }
            }else{
                awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                awsBo.set("OUTPARAMETER",response.toString());

                // 创建一条流程实例，用于存储DW查看
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.completeProcess接口");
                if (num == 0) {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), "admin");
                } else {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), targetUid);
                }
            }
            return response;
        }
    }

    //流程刷新接口
    @Mapping(value = "TASKSYNC.refreshProcess",desc = "流程刷新接口")
    public Response refreshProcess(@Param(value = "applicationName", desc = "系统标识", required = true) String applicationName,
                                    @Param(value = "outerId", desc = "待办ID", required = true) String outerId,
                                    @Param(value = "fundCode", desc = "产品代码", required = true) String fundCode,
                                    @Param(value = "extInfo", desc = "扩展信息", required = false) Map extInfo)
    {
        //将所有参数放入map中
        RequestData requestData = new RequestData();
        requestData.setApplicationName(applicationName);
        requestData.setOuterId(outerId);
        requestData.setTargetUid(fundCode);
        requestData.setExtInfo(extInfo);

        //定义返回值
        Response response = new Response();
        STSService service = new STSService();
        AwsLog awsLog = AwsLog.awsLog(applicationName, "TASKSYNC.refreshProcess",
                "流程刷新接口", null, JSON.toJSONString(requestData), applicationName, null, outerId,"","","","","", "","", JSON.toJSONString(extInfo));
        ResponseData responseData = new ResponseData();
        String code = null;
        String desc = null;
        Map<String, String> responseExtInfo = null;

        responseData.setExtInfo(responseExtInfo);

        try {
            //获取当前节点信息
            TaskInstance taskInstance = SDK.getTaskAPI().getTaskInstance(outerId);
            if (null!=taskInstance) {
                ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                response.setCode(successRC.getCode());
                response.setDesc(successRC.getDesc());
                responseData = service.refreshProcess(requestData, taskInstance);
                response.setData(responseData);
            } else {
                code = "500";
                desc = outerId+"不是任务实例id";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            }
        }catch (Exception e){
            code = "500";
            desc = "流程刷新错误";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);
        } finally {
            // 将错误信息存储至接口日志表中
            BO awsBo = awsLog.getBo();
            if(awsBo != null) {
                // awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                // awsBo.set("OUTPARAMETER",response.toString());

                // 创建一条流程实例，用于存储DW查看
                String targetUid = responseData.getTargetUid();
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.refreshProcess接口");
                SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), targetUid);
            }else {
                // awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                // awsBo.set("OUTPARAMETER",response.toString());
                awsLog.end("", response);

                // 创建一条流程实例，用于存储DW查看
                String targetUid = responseData.getTargetUid();
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.refreshProcess接口");

                SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), targetUid);
            }
            return response;
        }
    }

    //通知消息
    @Mapping(value = "TASKSYNC.notificationMessage",desc = "通知消息")
    public Response notificationMessage(@Param(value = "sendMsgType", desc = "消息发送类型", required = true) String sendMsgType,
                                      @Param(value = "applicationName", desc = "系统标识", required = true) String applicationName,
                                      @Param(value = "messageSender", desc = "消息发送人ID", required = true) String messageSender,
                                      @Param(value = "messageRecipientId", desc = "消息接收人ID", required = true) String messageRecipientId,
                                      @Param(value = "messageTitle", desc = "消息标题", required = true) String messageTitle,
                                      @Param(value = "messageContent", desc = "消息内容", required = true) String messageContent,
                                      @Param(value = "messageUrl", desc = "消息链接", required = false) String messageUrl,
                                      @Param(value = "extInfo", desc = "扩展信息", required = false) Map extInfo)
    {


        //定义返回值
        Response response = new Response();
        STSService service = new STSService();
        //将所有参数放入
        RequestData requestData = new RequestData();
        AwsLog awsLog = AwsLog.awsLog(applicationName, "TASKSYNC.notificationMessage","通知消息接口",
                messageSender, messageRecipientId, JSON.toJSONString(requestData), applicationName, null, null, null,"","","","","", JSON.toJSONString(extInfo));
        ResponseData responseData = new ResponseData();

        requestData.setSendMsgType(sendMsgType);
        requestData.setApplicationName(applicationName);
        requestData.setMessageSender(messageSender);
        requestData.setMessageRecipientId(messageRecipientId);
        requestData.setMessageTitle(messageTitle);
        requestData.setMessageContent(messageContent);
        requestData.setMessageUrl(messageUrl);
        requestData.setExtInfo(extInfo);


        String code = null;
        String desc = null;
        Map<String, String> responseExtInfo = null;

        responseData.setExtInfo(responseExtInfo);

        //查询创建人是否存在本系统中
        String  USERID = DBSql.getString("SELECT USERID FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{messageSender,messageSender});
        if(USERID != "" && !USERID.equals("")){
            messageSender = USERID;
        }else{
            //返回Reponse信息
            code = "500";
            desc = "BPM系统不存在创建人账户";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);
        }


        try {
            if (sendMsgType.equals("PC")) {

                //查询消息发送人是否存在本系统中
                int num = DBSql.getInt("SELECT COUNT(*) FROM ORGUSER WHERE USERID = ?", new Object[]{messageSender});
                //查询消息接收人是否存在本系统中
                int num1 = DBSql.getInt("SELECT COUNT(*) FROM ORGUSER WHERE USERID = ?", new Object[]{messageRecipientId});
                if (num == 0 && num1 == 0) {
                    //返回Reponse信息
                    code = "500";
                    desc = "BPM系统不存在消息发送人、接收人账户";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.ERROR, response);
                } else if (num == 0 && num1 != 0) {
                    //返回Reponse信息
                    code = "500";
                    desc = "BPM系统不存在消息发送人账户";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.ERROR, response);
                } else if (num != 0 && num1 == 0) {
                    //返回Reponse信息
                    code = "500";
                    desc = "BPM系统不存在消息接收人账户";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.ERROR, response);
                } else {
                    ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                    response.setCode(successRC.getCode());
                    response.setDesc(successRC.getDesc());
                    responseData = service.notificationMessagePC(requestData);
                    response.setData(responseData);
                }
            }
        }catch(Exception e){
                code = "500";
                desc = "通知消息接口错误";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            } finally{
            // 将错误信息存储至接口日志表中
            BO awsBo = awsLog.getBo();
            if(awsBo != null) {
                // awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                // awsBo.set("OUTPARAMETER",response.toString());

                // 创建一条流程实例，用于存储DW查看
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", messageSender, messageSender + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.notificationMessage接口");
                SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), messageSender);
            }else {
                // awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                // awsBo.set("OUTPARAMETER",response.toString());
                // awsLog.end("", response);

                // 创建一条流程实例，用于存储DW查看
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", messageSender, messageSender + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.notificationMessage接口");
                SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsBo, createBOProcessInstance.getId(), messageSender);

            }
                return response;
            }
    }

    //流程取消接口（终止）
    @Mapping(value = "TASKSYNC.terminateProcess",desc = "流程取消接口")
    public Response terminateProcess(@Param(value = "applicationName", desc = "待办系统标识", required = true) String applicationName,
                                        @Param(value = "targetUid", desc = "操作人ID", required = true) String targetUid,
                                        @Param(value = "processInstance", desc = "流程实例ID", required = true) String processInstance,
                                        @Param(value = "cancelReason", desc = "取消原因", required = false) String cancelReason,
                                        @Param(value = "extInfo", desc = "扩展信息", required = false) Map extInfo)
    {
        //定义返回值
        Response response = new Response();
        STSService service = new STSService();
        //将所有参数放入
        RequestData requestData = new RequestData();
        ResponseData responseData = new ResponseData();

        //定义操作人/日志录入人用户
        UserModel targetUser = null;
        requestData.setActionName(applicationName);
        requestData.setTargetUid(targetUid);
        requestData.setProcessInstId(processInstance);
        requestData.setCancelReason(cancelReason);
        requestData.setExtInfo(extInfo);
        SDK.getLogAPI().consoleInfo("打印取消任务的入参："+requestData.toString());
        AwsLog awsLog = AwsLog.awsLog(applicationName, "TASKSYNC.terminateProcess","流程取消（终止）接口",
                targetUid, null, JSON.toJSONString(requestData), applicationName, processInstance, null,null,"","","","","",JSON.toJSONString(extInfo));

        responseData.setProcess_InstID(processInstance);
        responseData.setTargetUid(targetUid);
        responseData.setIsCancle("cancelFailed");
        responseData.setExtInfo(extInfo);


        String code = null;
        String desc = null;
        Map<String, String> responseExtInfo = null;
        responseData.setExtInfo(responseExtInfo);
        try {
            //查询创建人是否存在本系统中
            String  USERID = DBSql.getString("SELECT USERID FROM ORGUSER WHERE USERID = ? OR USERNO = ?",new Object[]{targetUid,targetUid});
            if(USERID != "" && !USERID.equals("")){
                targetUid = USERID;
            }else{
                //返回Reponse信息
                code = "500";
                desc = "BPM系统不存在创建人账户";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
            }

            requestData.setTargetUid(targetUid);
            //判断流程实例是否存在BPM系统
            ProcessInstance getProcessInstance = SDK.getProcessQueryAPI().detailById(processInstance);
            if (null!=getProcessInstance) {
                //判断操作人是否存在BPM系统
                SDK.getLogAPI().consoleInfo("是否进入判断流程是否存在==="+getProcessInstance);
                targetUser = SDK.getORGAPI().getUser(targetUid);
                if (null!=targetUser) {
                    //判断流程实例是否已经结束
                    SDK.getLogAPI().consoleInfo("是否进入判断办理人是否存在==="+targetUser);
                    boolean endById = SDK.getProcessAPI().isEndById(processInstance);
                    if (!endById) {
                        //判断当前节点是否允许终止流程
                        SDK.getLogAPI().consoleInfo("是否进入判断流程任务是否终止==="+endById);
                        TaskInstance currentTask = SDK.getTaskQueryAPI().processInstId(processInstance).activeTask().detail();
                        String activityDefId = currentTask.getActivityDefId();
                        BO baBO = SDK.getBOAPI().query("BO_PS_BUSINESS_ACTIVITY", true).addQuery("ACTIVITY_DEFID = ", activityDefId).detail();
                        if (null!=baBO) {
                            String isallowterminate = baBO.getString("ISALLOWTERMINATE");
                            if (null==isallowterminate || isallowterminate.equals("") || isallowterminate.equals("01")) {
                                ResponseCode successRC = ResponseCode.SUCCESS_RESPONSE;
                                response.setCode(successRC.getCode());
                                response.setDesc(successRC.getDesc());
                                SDK.getLogAPI().consoleInfo("是否进入关闭流程任务操作==="+requestData);
                                responseData = service.terminateProcess(requestData);
                                SDK.getLogAPI().consoleInfo("是否结束关闭流程任务操作==="+requestData);
                                responseData.setIsCancle("cancel");
                                response.setData(responseData);
                                awsLog.end(AwsLog.getSUCCESS(), response);

                            } else {
                                code = "500";
                                desc = "当前流程节点不允许取消流程";
                                response.setCode(code);
                                response.setDesc(desc);
                                response.setData(responseData);
                                awsLog.end(AwsLog.ERROR, response);

                            }
                        } else {
                            code = "500";
                            desc = "当前流程节点未维护至流程节点与业务状态规则表";
                            response.setCode(code);
                            response.setDesc(desc);
                            response.setData(responseData);
                            awsLog.end(AwsLog.ERROR, response);

                        }
                    } else {
                        code = "500";
                        desc = "当前流程已经结束";
                        response.setCode(code);
                        response.setDesc(desc);
                        response.setData(responseData);
                        awsLog.end(AwsLog.ERROR, response);

                    }
                } else {
                    code = "500";
                    desc = "BPM系统不存在操作人用户";
                    response.setCode(code);
                    response.setDesc(desc);
                    response.setData(responseData);
                    awsLog.end(AwsLog.ERROR, response);

                }
            } else {
                code = "500";
                desc = "BPM系统不存在当前流程实例";
                response.setCode(code);
                response.setDesc(desc);
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);

            }
        }catch (Exception e){
            e.printStackTrace();
            code = "500";
            desc = "流程取消错误";
            response.setCode(code);
            response.setDesc(desc);
            response.setData(responseData);
            awsLog.end(AwsLog.ERROR, response);
        } finally {
            // 将错误信息存储至接口日志表中
            BO awsBo = awsLog.getBo();
            if(awsBo != null) {
                awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                awsBo.set("OUTPARAMETER",response.toString());
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.terminateProcess接口");

                // 创建一条流程实例，用于存储DW查看
                if (targetUser == null) {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsLog.getBo(), createBOProcessInstance.getId(), "admin");
                } else {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsLog.getBo(), createBOProcessInstance.getId(), targetUid);
                }
            }else {
                awsBo.set("INTERFACE_PARAMETER",requestData.toString());
                awsBo.set("OUTPARAMETER",response.toString());
                response.setData(responseData);
                awsLog.end(AwsLog.ERROR, response);
                ProcessInstance createBOProcessInstance = SDK.getProcessAPI().createBOProcessInstance("obj_d778147f5b9741138b427effafce0753", targetUid, targetUid + "在"
                        + AwsLog.sdf.format(awsLog.getCallStartTime()) + "调用了TASKSYNC.terminateProcess接口");

                // 创建一条流程实例，用于存储DW查看
                if (targetUser == null) {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsLog.getBo(), createBOProcessInstance.getId(), "admin");
                } else {
                    SDK.getBOAPI().create("BO_EU_INTERFACE_LOG", awsLog.getBo(), createBOProcessInstance.getId(), targetUid);
                }
            }
            return response;
        }
    }
}